


class Query {

    static TableName = '';
    static where = [];
    static limit = 0;
    static offset = 0;
    static order = [];
    static group = [];
    static having = [];
    static join = [];

    static operator = ['=', '!=', '>', '<', '>=', '<=', 'like', 'in', 'between'];
    constructor() {

    }

    TableName(tableName) {
        this.TableName = tableName;
        return this;
    }

    //  name , = , 张三
    where(field, ...args) {
        if (args.length === 0) {
            throw new Error('operator error');
        }
        if (args.length === 1) {
            this.where.push({ field, operator: '=', value: args[0] });
        } else if (args.length === 2) {
            if (this.operator.includes(args[0])) {
                this.where.push({ field, operator: args[0], value: args[1] });
            } else {
                this.where.push({ field, operator: '=', value: args[0] });
            }
        } else if (args.length >= 3) {
            throw new Error('operator error');
        }

        this.where.push({ field, ...args });
        return this;
    }

    limit(limit) {
        this.limit = limit;
        return this;
    }

    offset(offset) {
        this.offset = offset;
        return this;
    }

    order(order) {
        this.order = order;
        return this;
    }


    getSql(type = 'select') {


        switch (type) {
            case 'select':
                sql = `SELECT ${this.fields.join(',')} FROM ${this.TableName}`;
                break;
            case 'update':
                sql = `UPDATE ${this.TableName} SET ${data.map(item => `${item.field} = ${item.value}`).join(',')} WHERE ${this.getWhere()}`;
                break;
        }
        if (this.where.length > 0) {
            sql += ` WHERE ${this.getWhere()}`;
        }
        if (this.limit > 0) {
            sql += ` LIMIT ${this.limit}`;
        }
        return sql;
    }


    getWhere() {
        return this.where.map(item => `${item.field} ${item.operator} ${item.value}`).join(' AND ');
    }


    update(data) {
        let sql = `UPDATE ${this.TableName} SET ${data.map(item => `${item.field} = ${item.value}`).join(',')} WHERE ${this.getWhere()}`;
        return sql;
    }



}



const query = new Query();
query.TableName('user').where('id', 1).limit(1).execute();


module.exports = new Query();